package exam.model.dao;

import java.util.HashMap;
import java.util.List;

import exam.common.util.NullCheckUtil;
import exam.model.entity.TStudentEntity;
import exam.system.dao.AbstractDAO;

public class TStudentDAO extends AbstractDAO<TStudentEntity>{
	
	public TStudentDAO() {
		super(TStudentEntity.class);
	}
	
	public List<TStudentEntity> selectListStudent() {
		return super.selectList("select s from TStudentEntity s inner join s.userEntity");
	}
	
	public TStudentEntity selectByPk(long id) {
		return super.selectByPk(id);
	}
	
	public TStudentEntity selectByCd(String cd) {
		String query = "select s from TStudentEntity s inner join s.userEntity " +
				"where s.studentCd=:studentCd";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("studentCd", cd);
		return jdbcManager.executeByQueryParams(query, params).getSingleResult();
	}
	
	/**
	 * insertStudent
	 * @param student
	 * @return int
	 */
	public int insertStudent(TStudentEntity student) {
		return super.insert(student);
	}
	
	public int updateStudent(TStudentEntity tStudentEntity) {
		String query = "update TStudentEntity s set s.studentCd=:studentCd, s.firstName=:firstName, s.lastName=:lastName, " +
		"s.gender=:gender, s.birthday=:birthday, s.course=:course, s.studentClass=:studentClass," +
		"s.updateDate=:updateDate where s.studentId=:studentId";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("firstName", tStudentEntity.getFirstName());
		params.put("lastName", tStudentEntity.getLastName());
		params.put("gender", tStudentEntity.getGender());
		params.put("birthday", tStudentEntity.getBirthday());
		params.put("course", tStudentEntity.getCourse());
		params.put("studentClass", tStudentEntity.getStudentClass());
		params.put("updateDate", tStudentEntity.getUpdateDate());
		params.put("studentCd", tStudentEntity.getStudentCd());
		params.put("studentId", tStudentEntity.getStudentId());
		return jdbcManager.executeByQueryParams(query, params).getInt();
	}
	
	public boolean deleteStudentByPk(long id) {
		if (NullCheckUtil.isNull(selectByPk(id))) {
			return false;
		}
		super.delete(id);
		return true;
	}
	
	public int deleteStudentByCd(String cd) {
		String query = "delete from TStudentEntity s where s.studentCd=:studentCd";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("studentCd", cd);
		return super.jdbcManager.executeByQueryParams(query, params).getInt();
	}
	
	public int updateStudentByCd(TStudentEntity tStudentEntity) {
		String query = "update TStudentEntity s set s.studentCd=:studentCd, s.firstName=:firstName, s.lastName=:lastName, " +
				"s.gender=:gender, s.birthday=:birthday, s.course=:course, s.studentClass=:studentClass," +
				"s.updateDate=:updateDate where s.studentCd=:studentCd;";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("firstName", tStudentEntity.getFirstName());
		params.put("lastName", tStudentEntity.getLastName());
		params.put("gender", tStudentEntity.getGender());
		params.put("birthday", tStudentEntity.getBirthday());
		params.put("course", tStudentEntity.getCourse());
		params.put("studentClass", tStudentEntity.getStudentClass());
		params.put("updateDate", tStudentEntity.getCreateDate());
		params.put("studentCd", tStudentEntity.getStudentCd());
		return jdbcManager.executeByQueryParams(query, params).getInt();
	}
	
}
